package com.tcm.job.service;


import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.NumberUtil;
import com.base.core.lang.DateUtils;
import com.base.core.lang.LocalDateTimeUtils;
import com.tcm.job.api.service.OrderSettlementService;
import com.tcm.job.mapper.PlatformOrderBillMapper;
import com.tcm.job.mapper.SettlementOrderDayMapper;
import com.tcm.job.pojo.PlatformOrderBill;

import com.tcm.job.pojo.SettlementOrderDay;
import com.tcm.job.pojo.SettlementOrderMonth;
import org.apache.dubbo.config.annotation.DubboService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@DubboService(version = "0.0.1-alpha", timeout = 6000, retries = 3, delay = -1)
public class OrderSettlementServiceImpl implements OrderSettlementService {

    @Autowired
    private PlatformOrderBillMapper platformOrderBillMapper;

    @Autowired
    private SettlementOrderDayMapper settlementOrderDayMapper;


    /**
     * 平台日结算
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void settlementByDay() {

        //获取昨天最后一秒的时间做计算截止时间
        Date day = DateUtils.getOfDayFirst(new Date());
        Calendar calendarStart = Calendar.getInstance();
        calendarStart.setTime(day);
        calendarStart.add(Calendar.SECOND, -1);
        String endTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(calendarStart.getTime());
        Calendar calendarEnd = Calendar.getInstance();
        calendarEnd.setTime(day);
        calendarEnd.set(Calendar.DATE, calendarEnd.get(Calendar.DATE) - 1);// 让日期加1
        String startTime=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(calendarEnd.getTime());
        //查询在endTime之前的未结算的结算单
        List<PlatformOrderBill> platformOrderBills = platformOrderBillMapper.findByTime(startTime, endTime);
        if (platformOrderBills.size() == 0) {
            return;
        }
        SettlementOrderDay settlementOrderDay = new SettlementOrderDay();
        for (PlatformOrderBill platformOrderBill : platformOrderBills) {
            settlementOrderDay.setSpreadMoney(NumberUtil.add(settlementOrderDay.getSpreadMoney(), platformOrderBill.getSpreadMoney()));
            settlementOrderDay.setSecondMoney(NumberUtil.add(settlementOrderDay.getSecondMoney(), platformOrderBill.getSecondMoney()));
            settlementOrderDay.setBillingMoney(NumberUtil.add(settlementOrderDay.getBillingMoney(), platformOrderBill.getBillingMoney()));
            settlementOrderDay.setProfits(NumberUtil.add(settlementOrderDay.getProfits(), platformOrderBill.getProfits()));
            settlementOrderDay.setTotalMoney(NumberUtil.add(settlementOrderDay.getTotalMoney(), platformOrderBill.getTotalMoney()));
            platformOrderBill.setStatus("2");
            platformOrderBill.setUpdateTime(DateUtil.date());
            platformOrderBillMapper.update(platformOrderBill);
        }
        settlementOrderDay.setStatus(new Byte("0"));
        settlementOrderDay.setCreateTime(DateUtil.date());
        settlementOrderDay.setEndTime(DateUtil.date());
        settlementOrderDay.setDeleted(new Byte("0"));
        settlementOrderDayMapper.insert(settlementOrderDay);

    }

    /**
     * 月结算
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void settlementByMonth() {

        //获取上个月最后一秒的时间做计算截止时间
        String day = DateUtils.getPrevMonthLastDay();
        LocalDateTime endTime = LocalDateTimeUtils.parse(day+" 23:59:59");
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        //获取前月的第一天
        Calendar   cal_1=Calendar.getInstance();//获取当前日期
        cal_1.add(Calendar.MONTH, -1);
        cal_1.set(Calendar.DAY_OF_MONTH,1);//设置为1号,当前日期既为本月第一天
        String firstDay = format.format(cal_1.getTime());

        //获取前月的最后一天
        Calendar cale = Calendar.getInstance();
        cale.set(Calendar.DAY_OF_MONTH,0);//设置为1号,当前日期既为本月第一天
        String lastDay = format.format(cale.getTime());

        //查询在endTime之前的未结算的结算单
        List<PlatformOrderBill> platformOrderBills = platformOrderBillMapper.findByTime(firstDay,lastDay);
        if (platformOrderBills.size() == 0) {
            return;
        }
        SettlementOrderMonth settlementOrderMonth = new SettlementOrderMonth();
        settlementOrderMonth.setCreateTime(DateUtil.date());
        settlementOrderMonth.setEndTime(DateUtils.toDateYMDHMS(lastDay));
        for (PlatformOrderBill platformOrderBill : platformOrderBills) {
            settlementOrderMonth.setSecondMoney(NumberUtil.add(settlementOrderMonth.getSecondMoney(), platformOrderBill.getSecondMoney()));
            settlementOrderMonth.setBillingMoney(NumberUtil.add(settlementOrderMonth.getBillingMoney(), platformOrderBill.getBillingMoney()));
            settlementOrderMonth.setProfits(NumberUtil.add(settlementOrderMonth.getProfits(), platformOrderBill.getProfits()));
            settlementOrderMonth.setTotalMoney(NumberUtil.add(settlementOrderMonth.getTotalMoney(), platformOrderBill.getTotalMoney()));
            platformOrderBill.setStatus("2");
            platformOrderBill.setUpdateTime(DateUtil.date());
            platformOrderBillMapper.update(platformOrderBill);
        }
    }


}
